package com.xl.checkshake

import android.util.Log
import java.util.concurrent.ConcurrentHashMap

object Checkshake {
    private const val TAG = "Checkshake"
    private val concurrentHashMap: ConcurrentHashMap<Int, XLock?> = ConcurrentHashMap()

    fun getLock(id: Int, result: () -> Unit) {
        getLock(id, 2, result)
    }


    fun getLock(id: Int, time: Long, result: () -> Unit) {
        if (concurrentHashMap.containsKey(id)) {
            val xLock = concurrentHashMap[id]
            if (xLock?.islock() == true) {
                Log.i(TAG, "被拦截")
                return
            }
            xLock?.lock()
        } else {
            val xLock = XLockFactory.createLock(time)
            concurrentHashMap[id] = xLock
            xLock.lock()
        }
        return result()
    }
}